package www.superbytecode.com.controller;


import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import www.superbytecode.com.iframe.UIAlert;
import www.superbytecode.com.model.DBGenerator;
import www.superbytecode.com.util.ConfigHelper;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName superbytecode-builder
 * @Package www.superbytecode.com.controller
 * @ClassName GeneratorConfigController
 * @Description
 * @Author [Lance Ting]
 * @Date 2020/7/14 20:37
 * @Version V1.0
 * @Copyright: 2020 www.super-bytecode.com Inc. All rights reserved.
 * @TODO：本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
public class GeneratorConfigController  extends BaseFXController{

    private static final Logger logger = LoggerFactory.getLogger(GeneratorConfigController.class);

    @FXML
    private TableView<DBGenerator> configTable;
    @FXML
    private TableColumn nameColumn;
    @FXML
    private TableColumn opsColumn;

    private MainUIController mainUIController;

    private GeneratorConfigController controller;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        controller = this;
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        // 自定义操作列
        opsColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        opsColumn.setCellFactory(cell -> { return new TableCell() {
            @Override
            protected void updateItem(Object item, boolean empty) {
                super.updateItem(item, empty);
                if (item == null || empty) {
                    setText(null);
                    setGraphic(null);
                } else {
                    Button btn1 = new Button("应用");
                    Button btn2 = new Button("删除");
                    HBox hBox = new HBox();
                    hBox.setSpacing(10);
                    hBox.getChildren().add(btn1);
                    hBox.getChildren().add(btn2);
                    btn1.setOnAction(event -> {
                        try {
                            // 应用配置
                            DBGenerator DBGenerator = ConfigHelper.loadGeneratorConfig(item.toString());
                            mainUIController.setGeneratorConfigIntoUI(DBGenerator);
                            controller.closeDialogStage();
                        } catch (Exception e) {
                            UIAlert.showErrorAlert(e.getMessage());
                        }
                    });
                    btn2.setOnAction(event -> {
                        try {
                            // 删除配置
                            logger.debug("item: {}", item);
                            ConfigHelper.deleteGeneratorConfig(item.toString());
                            refreshTableView();
                        } catch (Exception e) {
                            UIAlert.showErrorAlert(e.getMessage());
                        }
                    });
                    setGraphic(hBox);
                }
                }
            };
        });
        refreshTableView();
    }

    public void refreshTableView() {
        try {
            List<DBGenerator> configs = ConfigHelper.loadGeneratorConfigs();
            configTable.setItems(FXCollections.observableList(configs));
        } catch (Exception e) {
            UIAlert.showErrorAlert(e.getMessage());
        }
    }

    void setMainUIController(MainUIController mainUIController) {
        this.mainUIController = mainUIController;
    }

}